package com.example.cloudmaps;


import java.util.List;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.maps.GeoPoint;

import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;

public class NavigationPage extends FragmentActivity {

String username="";



	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_navigation_page);
		
		
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.navigation_page, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.action_logout:
	        	new Logout().execute();
	        	Intent myIntent3 = new Intent(NavigationPage.this, LoginActivity.class);
				startActivityForResult(myIntent3, 0);
				NavigationPage.this.finish();
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	@Override
	protected void onResume(){
		super.onResume();
		new getLoginInfo().execute();
		
	}
	
	//Get Login Info
		private class getLoginInfo extends AsyncTask<Void, Void, String>{
			@Override
			protected String doInBackground(Void... nothing) {
				
				MapDataSource mds2 = new MapDataSource(NavigationPage.this);
				
				mds2.open();
				
				try {
					username = mds2.getUsername();
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				mds2.close();
				
				return username;
			}
			
			@Override
			protected void onPostExecute(String whatever) {
				if (username.equals("user")){
					Intent myIntent = new Intent(NavigationPage.this, LoginActivity.class);
					startActivityForResult(myIntent, 0);
					return;
				}
				else{
					return;
				}
			}
		}
		
		//Logout
		private class Logout extends AsyncTask<Void, Void, String>{
			@Override
			protected String doInBackground(Void... nothing) {
				
				MapDataSource mds2 = new MapDataSource(NavigationPage.this);
				
				mds2.open();
				
				try {
					mds2.logoutUser();
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				mds2.close();
				
				return "";
			}
			
			@Override
			protected void onPostExecute(String whatever) {
					Intent myIntent = new Intent(NavigationPage.this, LoginActivity.class);
					startActivityForResult(myIntent, 0);
					NavigationPage.this.finish();
			}
			
		}

		
}
